package com.xymtop.api.utils;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * @ClassName : MyCookie
 * @Description : 关于cookie的操作
 * @Author : 肖叶茂
 * @Date: 2022/12/3  16:18
 */
public class MyCookie {
    /**
    *@Description:获取cookie
    *@Parameter:[name]
    *@Return:java.lang.String
    *@Author:肖叶茂
    *@Date:2022/12/3
    **/
    public  static  String getCookie(HttpServletRequest request, String name){

        System.out.println(request.getCookies());
        Cookie[] cookies =  request.getCookies();

        if(cookies != null){
            for(Cookie cookie : cookies){

                if(cookie.getName().equals(name)){
                    return cookie.getValue();
                }
            }
        }

        return  null;
    }
    
    /**
    *@Description:设置cookie
    *@Parameter:[response, name, value]
    *@Return:void
    *@Author:肖叶茂
    *@Date:2022/12/3
    **/
    public  static  void setCookie(HttpServletResponse response, String name, String value){

        Cookie cookie = new Cookie(name, value); //key 为cookie内容名字，value为cookie 类型
        cookie.setMaxAge(1000*60*60*24);       //cookie过期时间

        cookie.setPath("/");
        cookie.setSecure(true);
        cookie.setHttpOnly(true);
        response.addCookie(cookie);
    }
}
